<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="left">
        <!-- 黑马logo -->
        <a href="javascript:;" @click="toHome">
          <img src="../assets/heima.png" alt="首页" />
        </a>
        <!-- 顶部标题 -->
        <h2>电商后台管理系统</h2>
      </div>
      <div class="right">
        <el-button @click="logout">退出登录</el-button>
      </div>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '300' : '64'">
        <el-button @click="isCollapse = !isCollapse" class="collapseBtn"
          >|||</el-button
        >
        <!-- 侧边栏菜单 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :collapse="isCollapse"
          :collapse-transition="collapseTransition"
          :unique-opened="uniqueOpened"
          :default-active="activepath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="item.iconClass"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级子菜单 -->
            <el-menu-item
              :index="subItem.path + ''"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="changePath('/' + subItem.path)"
            >
              <!-- 二级菜单模板 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="subItem.iconClass"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体结构 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { tokenName } from '@/config/env.js'
import { getMenusAPI } from '@/api'

export default {
  name: 'Home',
  data() {
    return {
      menuList: null,
      isCollapse: false, // 是否折叠菜单
      uniqueOpened: true,
      collapseTransition: false,
      activepath: ''
    }
  },
  components: {},
  created() {
    this.getMenuList()
    this.apath = window.sessionStorage.getItem('activePath')
    // this.changePath(this.apath);
  },
  methods: {
    logout() {
      if (!confirm('确认退出登录吗?')) return

      sessionStorage.removeItem(tokenName)
      this.$router.replace('/login')
    },
    async getMenuList() {
      // 发送请求获取左侧菜单数据
      const { data: res } = await getMenusAPI()
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)

      this.menuList = res.data
      // console.log(res)
    },
    changePath(apath) {
      // console.log(this.$route.path)//home/welcome
      if ('/home' + apath === this.$route.path) return
      this.activepath = apath
      window.sessionStorage.setItem('activePath', apath)
      this.$router.push('/home' + apath)
    },
    toHome() {
      if (this.$route.path === '/home/manage') return
      this.activepath = '/home'
      window.sessionStorage.setItem('activePath', '/home')
      this.$router.push('/home')
    }
  }
}
</script>

<style scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #333744;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
.left {
  display: flex;
}

.el-header h2 {
  color: white;
  margin-left: 10px;
}

.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 5px;
}

.el-menu-item {
  border-right: none;
}

.collapseBtn {
  width: 100%;
}
</style>
